package org.goj.practice.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.entity.practice.Classify;
import org.goj.common.entity.practice.Problem;
import org.goj.common.model.Result;
import org.goj.practice.service.ClassifyService;
import org.goj.practice.service.ProblemService;
import org.goj.practice.vo.ClassifyCascaderVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 题目分类 前端控制器
 * </p>
 *
 * @author CK
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/classify")
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @Resource
    private ProblemService problemService;

    /**
     * 用户查询分类
     *
     * @return Result<List < ClassifyCascaderVO>>
     * @author CK
     * @date 2021/5/4 16:52
     */
    @GetMapping("/listCascader")
    public Result<List<ClassifyCascaderVO>> getAllClassify() {
        List<ClassifyCascaderVO> classify = classifyService.listClassifyCascader();
        Assert.notNull(classify, "查询分类失败！");
        return Result.succeed("查询分类成功！", classify);
    }

    /**
     * 管理员添加一条分类
     *
     * @param classifyParentId 分类父ID
     * @param classifyName     分类名
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/4 17:02
     */
    @PostMapping("/insertClassify")
    public Result<Boolean> insertClassify(
            @RequestParam(value = "classifyParentId") Integer classifyParentId,
            @RequestParam(value = "classifyName") String classifyName) {
        classifyService.insertClassify(classifyParentId, classifyName);
        return Result.succeed("添加分类成功", true);
    }

    /**
     * 管理员更新分类名称
     *
     * @param classifyName 分类名称
     * @param classifyId   分类ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/4 19:59
     */
    @PostMapping("/updateClassify")
    public Result<Boolean> updateClassify(
            @RequestParam(value = "classifyName") String classifyName,
            @RequestParam(value = "classifyId") Integer classifyId) {
        Classify classify = new Classify();
        classify.setClassifyId(classifyId).setClassifyName(classifyName);
        boolean flag = classifyService.updateById(classify);
        Assert.isTrue(flag, "更新分类名称失败");
        return Result.succeed("更新分类名称成功", flag);
    }

    /**
     * 管理员分页查询分类
     *
     * @param current 当前页
     * @param size    页面大小
     * @return Result<Page < Classify>>
     * @author CK
     * @date 2021/5/4 17:56
     */
    @GetMapping("/listClassifyAdmin")
    public Result<Page<Classify>> listClassifyAdmin(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Classify> page = new Page<>(current, size);
        return Result.succeed("查询分类列表成功", classifyService.page(page));
    }

    /**
     * 管理员查询父ID集合
     *
     * @return Result<List < Classify>>
     * @author CK
     * @date 2021/5/4 18:16
     */
    @GetMapping("/listClassifyParent")
    public Result<List<Classify>> listClassifyParent() {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("classify_parent_id", 10);
        List<Classify> list = classifyService.list(queryWrapper);
        Classify classify = new Classify();
        classify.setClassifyId(0).setClassifyName("零层");
        list.add(0, classify);
        return Result.succeed("查询父ID集合成功", list);
    }

    /**
     * 管理员删除分类
     *
     * @param classifyId 分类ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/4 19:00
     */
    @PostMapping("/deleteClassify")
    public Result<Boolean> deleteClassify(@RequestParam(value = "classifyId") Integer classifyId) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classify_parent_id", classifyId);
        List<Classify> list = classifyService.list(queryWrapper);
        Assert.isTrue(list.size() == 0, "该分类存在子分类");

        QueryWrapper<Problem> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("problem_id").eq("classify_id", classifyId);
        int count = problemService.count(queryWrapper1);
        Assert.isTrue(count == 0, "该分类下存在题目");

        boolean flag = classifyService.removeById(classifyId);
        Assert.isTrue(flag, "删除分类失败");
        return Result.succeed("删除分类成功", flag);
    }

}

